package com.example.experiment08;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置全屏显示
        VideoView video =(VideoView)findViewById(R.id.video);//获取VideoView组件

        //加载要播放的视频
        File file=new File(Environment.getExternalStorageDirectory().toString()+"/video.mp4");//获取文件对象
        if(file.exists()){
            video.setVideoPath(file.getAbsolutePath());//指定要播放的视频

        }else {
            Toast.makeText(MainActivity.this,"要播放的视频文件不存在",Toast.LENGTH_SHORT).show();
        }

        //控制视频的播放
        android.widget.MediaController mc=new android.widget.MediaController(MainActivity.this);
        //创建MediaController对象
        video.setMediaController(mc);//让VideoView和MediaController关联
        video.requestFocus();//让VideoView获得焦点
        video.start();//开始播放视频
        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(MainActivity.this,"视频播放完毕",Toast.LENGTH_SHORT).show();
            }
        });


    }
}

